using CrazyDogToDo.Entity.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore.Design.Internal;

namespace CrazyDogToDo.Service.Services
{
    public class UserService
    {
        private readonly CrazyDogToDoContext _crazy = new CrazyDogToDoContext();
     
        /// <summary>
        /// 用户登录
        /// </summary>  
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Login(string email, string password)
        {
            var user = _crazy.User.SingleOrDefault(u => u.Email.Equals(email) && u.Password.Equals(password));
            if (user == null)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Register(string email, string nickName, string password, bool gender, int score)
        {
            var user = _crazy.User.SingleOrDefault(u => u.Email.Equals(email));
            if (user != null)
                return false;
            else
            {
                user = new User()
                {
                    Email = email,
                    NickName = nickName,
                    Password = password,
                    Gender = gender,
                    Score = 0,
                    UserAvatar = "/User/img/UserAvatar/头像.png"
                };
                Inventory inventory = new Inventory()
                {
                    Email = email,
                    Title = "我的活动",
                    CreateTime = DateTime.Now
                };
                _crazy.User.Add(user);
                _crazy.Inventory.Add(inventory);
                _crazy.SaveChanges();
                return true;
            }

        }
        /// <summary>
        /// 修改用户昵称
        /// </summary>
        /// <param name="email"></param>
        /// <param name="nickName"></param>
        public void ModifyNickName(string email, string nickName)
        {
            var user = _crazy.User.SingleOrDefault(u => u.Email.Equals(email));
            user.NickName = nickName;
            _crazy.SaveChanges();
        }
        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        public bool ModifyPassword(string email, string password, string newPassword)
        {
            var user = _crazy.User.SingleOrDefault(u => u.Email.Equals(email) && u.Password.Equals(password));
            if (user == null)
                return false;
            user.Password = newPassword;
            _crazy.SaveChanges();
            return true;
        }
        /// <summary>
        /// 根据 email 找到用户
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public User FindUserByEmail(string email)
        {
            var user = _crazy.User.SingleOrDefault(u => u.Email.Equals(email));
            return user;
        }

        
        /// <summary>
        /// 修改用户头像
        /// </summary>
        /// <param name="email"></param>
        /// <param name="employeeId"></param>
        /// <param name="imgBase"></param>
        /// <returns></returns>
        public bool ChangeUserAvatar(string email, string imgBase)
        {            
            try
            {
                var user = _crazy.User.SingleOrDefault(u => u.Email == email);
                var img = imgBase.Split(',');
                var bt = Convert.FromBase64String(img[1]);
                var myGuid = Guid.NewGuid().ToString();
                var path = "wwwroot/User/img/UserAvatar/" +
                              myGuid + ".png";
                var dataPath = "/User/img/UserAvatar/" + myGuid + ".png";
                File.WriteAllBytes(path, bt);
                user.UserAvatar = dataPath;
                _crazy.SaveChanges();
                var newUser = _crazy.User.SingleOrDefault(u => u.Email == email);               
               
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }




    }
}
